package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.PERecipeListAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpEntoty;
import com.glory.wxelectricitypda.bean.RecipeListEntity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class RecipePEDownloadActivity extends BaseDialogActivity {


    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.rvPEList)
    RecyclerView rvPEList;

    private ArrayList<String> recipeList = new ArrayList<>();
    private PERecipeListAdapter mPERecipeListAdapter;
    private String recipeType = "2";
    private String status = "2";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_download_pe;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("PE下载Recipe");
        freeUI_titleView.setRightListener("下载", new View.OnClickListener() {
            @Override
            public void onClick(View s) {
                DownloadRecipeById();
            }
        });

        rvPEList.setLayoutManager(new LinearLayoutManager(this));
        rvPEList.setItemAnimator(new DefaultItemAnimator());
        rvPEList.addItemDecoration(new DividerItemDecoration(this, 1));

        mPERecipeListAdapter = new PERecipeListAdapter(null);
        mPERecipeListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mPERecipeListAdapter.isFirstOnly(true);
        rvPEList.setAdapter(mPERecipeListAdapter);

    }


    @Override
    protected void initData() {

        etEqpId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    requestEqpInfo(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestEqpInfo(final String eqpID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getEqpInfo","GetEqpInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpEntoty>>(
                new TypeToken<ResponseBean<EqpEntoty>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<EqpEntoty>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    tvEqpType.setText(response.body().getBody().getEqpType());
                }else {
                    tvEqpType.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<EqpEntoty>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpInfo");
            }
        });
    }

    private void getEqpRecipeList(String eqpID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID);
            jsonObject.put("EqpType", tvEqpType.getText().toString());
            jsonObject.put("RecipeType", recipeType);
            jsonObject.put("Status", status);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("queryRecipeList","QueryRecipeList",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<RecipeListEntity>>(
                new TypeToken<ResponseBean<RecipeListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<RecipeListEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    mPERecipeListAdapter.setNewData(response.body().getBody().getList());
                }else {
                    mPERecipeListAdapter.setNewData(null);
                }
            }

            @Override
            public void onError(Response<ResponseBean<RecipeListEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpRecipeList");
            }
        });
    }

    private void DownloadRecipeById() {
        JSONObject jsonObject = new JSONObject();
        try {
            RecipeListEntity.RecipeEntity selectorData = mPERecipeListAdapter.getSelectorData();
            jsonObject.put("EqpID", etEqpId.getText().toString());
            jsonObject.put("RecipeName", selectorData.getRecipeName());
            jsonObject.put("RecipeID", selectorData.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("downloadRecipeById","DownloadRecipeById",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("Recipe下载成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "DownloadRecipeById");
            }
        });
    }

    @OnClick({R.id.rbTypeEQP, R.id.rbTypeGLOD,R.id.rbStatus2,R.id.rbStatus1,R.id.btnLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbTypeEQP:
                recipeType = "2";
                break;
            case R.id.rbTypeGLOD:
                recipeType = "3";
            case R.id.rbStatus2:
                status = "2";
                break;
            case R.id.rbStatus1:
                status = "";
                break;
            case R.id.btnLogin:
                if (etEqpId.getText().toString().equals("")){
                    showToast("请先扫描设备",false);
                    return;
                }
                getEqpRecipeList(etEqpId.getText().toString());
                break;
        }
    }

}
